package mm.apk.libmm.ui.activity

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleRegistry
import mm.apk.libmm.presenter.LifecycleCallback
import java.util.ArrayList

/**
 * Descriptions：
 * StartVersion：
 * <p>
 * Author：ChenME
 * Date：2019-11-15
 * Email：ibelieve1210@163.com
 */
open class BaseActivity : AppCompatActivity() {
    private var mLifecycleObservers: ArrayList<LifecycleCallback> = ArrayList()
    private var mLifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)

    /** *************************************************** *************************************************** */

    protected open fun prepareData() {}
    protected open fun initView() {}
    protected open fun initListener() {}
    protected open fun loadData() {}

    /** *************************************************** *************************************************** */

    override fun onDestroy() {
        super.onDestroy()
        removeObservers()
    }




    /** *************************************************** *************************************************** */

    override fun getLifecycle(): Lifecycle {
        return mLifecycleRegistry
    }

    fun addLifecycleObserver(lifecycleCallback: LifecycleCallback) {
        if (!mLifecycleObservers.contains(lifecycleCallback)) {
            mLifecycleObservers.add(lifecycleCallback)
        }
        mLifecycleRegistry.addObserver(lifecycleCallback)
    }

    private fun removeObservers() {
        for (observer in mLifecycleObservers) {
            mLifecycleRegistry.removeObserver(observer)
        }
        mLifecycleObservers.clear()
    }


    /** *************************************************** *************************************************** */
    /** *************************************************** *************************************************** */
}